Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Understanding The Object Relation Model

The Object Relation Model (ORM) attempts to fuse object-orientation with the relational database model. Because many of today’s programming languages, such as Java, are object-oriented, a tighter integration between the two would provide easier abstraction for developers who program in object-oriented languages and also are required to “program” in SQL. Such an integration would also relieve the necessity of constant translation between database tables and object-oriented data structures, which can be an arduous task.

Mapping A Table Into A Java Object

Let’s look at a simple example to demonstrate the basics of ORM. Suppose we create the following table in a database:

First_Name Last_Name Phone_Number Employee_Number
Pratik Patel 800-555-1212 30122
Karl Moss 800-555-1213 30124
Keith Weiskamp 800-555-1214 09249
Ron Pronk 800-555-1215 10464

You can easily map this table into a Java object. Here’s the Java code you would write to encapsulate the data contained in the table:

class Employee { 
int Key; 
String First_Name; 
String Last_Name; 
String Phone_Number; 
int Employee_Number; 
Key=Employee_Number; 
} 

To retrieve this table from the database into Java, we simply assign the respective columns to the Employee object we created previously for each row we retrieve, as shown here:

Employee emp_object = new Employee(); 
emp_object.First_Name= resultset.getString("First_Name"); 
emp_object.Last_Name= resultset.getString("Last_Name"); 
emp_object.Phone_Number=resultset.getString("Phone_Number"); 
emp_object.Employee_Number=resultset.getInt("Employee_Number"); 

With a larger database model (with links between tables), a number of problems can arise, including scalability due to multiple JOINs in the data model and cross-linking of table keys. Fortunately, a number of products are already available that allow you to create these kinds of object-oriented/relational bridges. Moreover, there are several solutions being developed to work specifically with Java.

I’ve given you an idea of what ORM is all about. If you would like to investigate this topic further, check out The Coriolis Group Web site (http://www.coriolis.com/jdbc-book) for links to ORM vendors and some really informative ORM documents. The ODMG (Object Database Management Group) is a consortium that is working on a revised standard for object database technology and the incorporation of this concept into programming languages such as Java. A link to the consortium’s Web site can be found on The Coriolis Group Web site as well.

Summary

As you can see from this brief chapter, mapping SQL data types to Java is truly a snap. We covered a few of the more important methods you will use to retrieve data from a database. For a complete reference, see Chapter 12 and have a look at the Date, Time, TimeStamp, Types, and Numeric classes.

The next chapter steps back from the JDBC to look at ways of presenting your data in Java. Using Java packages available on the Net, we’ll cover graphs, tables, and more. We’ll also discuss some nifty methods in the JDBC that will help streamline your code for retrieving data from your database.


Previous Table of Contents Next